/// <reference types="vitest" />
import { defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import legacy from '@vitejs/plugin-legacy'

function resolve(relativePath: string) {
  return path.resolve(__dirname, relativePath)
}

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': resolve('src')
    }
  },
  test: {
    // jsdom 通过提供 Browser API 模拟浏览器环境；默认 node 环境
    environment: 'jsdom'
    // deps: {
    //   inline: true // 每个依赖项都将被内联
    // }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 全局引入变量和 mixin
        // 文档上写的是additionalData 注意：在 sass-loader v8 中，这个选项名是 "prependData"
        additionalData: `
          @use "@/styles/variables.scss" as *;
          @use "@/styles/mixins.scss" as *;
        `,
        charset: false // 禁止项目scss添加@charset:UTF-8
      }
    },
    // postcss 删除库里的@charset:UTF-8
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: atRule => {
              if (atRule.name === 'charset') {
                atRule.remove()
              }
            }
          }
        }
      ]
    }
  },
  plugins: [vue(), vueJsx(), legacy()]
})
